package other.xsy.passlys.contract;

import com.rkhd.platform.sdk.data.model.Contract;
import com.rkhd.platform.sdk.data.model.Opportunity;
import com.rkhd.platform.sdk.log.Logger;
import com.rkhd.platform.sdk.log.LoggerFactory;
import com.rkhd.platform.sdk.model.XObject;
import com.rkhd.platform.sdk.trigger.DataResult;
import com.rkhd.platform.sdk.trigger.Trigger;
import com.rkhd.platform.sdk.trigger.TriggerRequest;
import com.rkhd.platform.sdk.trigger.TriggerResponse;
import org.apache.commons.lang.StringUtils;
import other.xsy.passlys.util.HCXObjectService;

import java.util.*;

/**
 * @Description  创建合同前进行判断
 * @Author 黎炎松
 * 2024-09-18 10:54
 */
public class ContractAddCheck implements Trigger {
    private static HCXObjectService hcxObjectService = new HCXObjectService();
    private static final Logger logger = LoggerFactory.getLogger();
    private static String LOGGER_MSG = "创建合同前进行判断 ==> ";

    /**
     * Trigger 事件处理
     * @param request
     * @return
     */
    @Override
    public TriggerResponse execute(TriggerRequest request) {
        List<XObject> dataList = request.getDataList();
        List<DataResult> dataResultList = new ArrayList<>();
        List<Contract> contracts = new ArrayList<>();
        for (XObject xObject : dataList) {
            Contract contract = (Contract) xObject;
            contracts.add(contract);
            dataResultList.add(new DataResult(true, "", contract));
        }
        dataResultList.set(0, checkContract(contracts.get(0)));
        return new TriggerResponse(true, "", dataResultList);
    }

    public static void main(String[] args) throws Exception {
        ContractAddCheck contractAddCheck = new ContractAddCheck();

        Contract contract = new Contract();
        contract.setId(3433410617858726L);
        contract.setOpportunityId(3427876294400603L);
        contract.setEntityType(3344120169699944L);
        //contract = hcxObjectService.get(contract);
        contractAddCheck.checkContract(contract);
    }

    /**
     * 创建合同前进行判断
     * @param contract
     * @return
     */
    public DataResult checkContract(Contract contract){
        try {
            logger.info(LOGGER_MSG + " 处理开始:" + contract);
            Long entityType = contract.getEntityType();
            Long opportunityId = contract.getOpportunityId(); // 业务机会
            Long quote = contract.getOffer__c(); // 报价单

            // 只判断业务类型为 项目订单: 3309750193298054 预交付订单: 3309752318565039
            List<Long> dealTypeList = Arrays.asList(3343981728602779L, 3344120169699944L);
            if (!dealTypeList.contains(entityType)) {
                logger.info(LOGGER_MSG + "当前合同业务类型无需处理");
                return new DataResult(true,"当前合同业务类型无需处理", contract);
            }

            // 判断当前业务机会是否存在预交付项目合同，存在则不允许新增
            if (entityType == 3344120169699944L) {
                String sql = String.format("select id from contract where opportunityId = %s and entityType = %s ", opportunityId, 3343981728602779L);
                Contract searchContract = hcxObjectService.queryOne(sql, Contract.class);
                if (searchContract != null) {
                    logger.error(LOGGER_MSG + "当前业务机会下已有[项目合同]，不允许新增[预交付合同]");
                    return new DataResult(false,"当前业务机会下已有[项目合同]，不允许新增[预交付合同]" , contract);
                }
            }

            // 是否有选报价单，有则无需处理
            if (quote != null) {
                logger.info(LOGGER_MSG + "当前合同有选择报价单，无需处理");
                return new DataResult(true,"当前合同有选择报价单，无需处理", contract);
            }

            // 获取当前业务机会是否有审批通过的报价单
            String sql = "select id from quote where oaApprovalStatus__c in (3,5) and quotationEntityRelOpportunity = " + opportunityId;
            XObject xObject = hcxObjectService.queryOne(sql, XObject.class);
            if (xObject != null) {
                logger.error(LOGGER_MSG + "当前业务机会有审批中和审批通过的报价单，需要进行选择");
                return new DataResult(false,"请选择报价单 (当前业务机会下有审批中和审批通过的报价单)" , contract);
            }

            // 如果没有选择报价单需要判断当前业务机会是否有 PMS项目编码
            Opportunity opportunity = new Opportunity();
            opportunity.setId(opportunityId);
            opportunity = hcxObjectService.get(opportunity);
            if (StringUtils.isBlank(opportunity.getPmsProjectCode__c())) {
                logger.error(LOGGER_MSG + "当前业务机会没有PMS项目编码");
                return new DataResult(false,"请先完成PMS概算审核，谢谢！" , contract);
            }

            // 允许创建
            logger.info(LOGGER_MSG + "运行新增");
            return new DataResult(true,"" , contract);
        } catch (Exception e) {
            logger.error(LOGGER_MSG + "校验异常:" + e.getMessage());
            return new DataResult(false, "校验异常:" + e.getMessage(), contract);
        }
    }

}
